package com.lemon.smartsafetynew.ui.widget;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.electrical.StandardUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 2018/3/16.
 */

public class SortView extends View {

    private Paint imagePaint;
    private Paint textPaint;

    private List<CircleDataVo> dataList = new ArrayList<>();
    private Float[] sizeArray = new Float[]{
            0.25f, 0.23f, 0.21f, 0.19f, 0.17f
    };

    private Float[] offsetStatus = new Float[]{
            1f, -1f, -1f, -1f, -1f
    };

    private static final float SHAKE_OFFSET = 5f;
    private static final float BOUNDARY_OFFSET = 20f;


    private float coefficient;

    private int height;
    private int width;

    public SortView(Context context) {
        super(context);
        initView();
    }

    public SortView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SortView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setDataList(List<CircleDataVo> dataList) {
        this.dataList = dataList;
        startAnimation();
    }

    private void initView() {
        imagePaint = new Paint();
        textPaint = new Paint();
//        imagePaint.setColor(Color.parseColor("#6572D4"));
//        imagePaint.setAntiAlias(true);
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(CommonUtil.convertDipOrPx(IApplication.context(), 10));
    }

    public void startAnimation() {

        final ValueAnimator anim = ValueAnimator.ofFloat(-1f, 1f, -1f);

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                coefficient = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        anim.setDuration(4000);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.start();
    }

    public static class CircleDataVo {

        private String key;   //传感器类型
        private String value; //传感器数值
        private String times; //此数值出现的次数

        public CircleDataVo(String key, String value, String times) {
            this.key = key;
            this.value = value;
            this.times = times;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getTimes() {
            return times;
        }

        public void setTimes(String times) {
            this.times = times;
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.WHITE);//设置画布背景颜色
        if (dataList != null) {
            for (int i = 0; i < dataList.size(); i++) {

                float radius = height * sizeArray[i];
                //float x = getWidth() / 2;
                //float y = getHeight() / 2;
                float x = xPoint(i);
                float y = yPoint(i);

                //imagePaint.setColor(StandardUtil.getElectricalStatusColor(dataList.get(i).getKey(), Float.valueOf(dataList.get(i).getValue())));
                imagePaint.setColor(Color.parseColor("#448ad0"));
                imagePaint.setAntiAlias(true);

                canvas.drawCircle(x + SHAKE_OFFSET * coefficient * offsetStatus[i], y, radius, imagePaint);
                Rect targetRect = new Rect((int) (x + 10 * coefficient * offsetStatus[i] - radius),
                        (int) (y - radius),
                        (int) (x + SHAKE_OFFSET * coefficient * offsetStatus[i] + radius),
                        (int) (y + radius));

                Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
                int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
                // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
                textPaint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(dataList.get(i).getValue() + "\r\n" + dataList.get(i).getTimes(), targetRect.centerX(), baseline, textPaint);

            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(changed) {
            height = getHeight();
            width = getWidth();
        }
    }


    private float xPoint(int position) {
        switch (position) {
            case 0:
                return width / 2;
            case 1:
                return height * sizeArray[position] + BOUNDARY_OFFSET;
            case 2:
                return width - height * sizeArray[position] - BOUNDARY_OFFSET;
            case 3:
                return height * sizeArray[position] + BOUNDARY_OFFSET + CommonUtil.convertDipOrPx(IApplication.context(), 50);
            case 4:
                return width - height * sizeArray[position] - BOUNDARY_OFFSET - CommonUtil.convertDipOrPx(IApplication.context(), 50);
            default:
                return -100f;
        }
    }

    private float yPoint(int position) {
        switch (position) {
            case 0:
                return height / 2;
            case 1:
                return height * sizeArray[position] + BOUNDARY_OFFSET;
            case 2:
                return height - height * sizeArray[position] - BOUNDARY_OFFSET;
            case 3:
                return height - height * sizeArray[position] - BOUNDARY_OFFSET;
            case 4:
                return height * sizeArray[position] + BOUNDARY_OFFSET;
            default:
                return -100f;
        }
    }
}
